package com.yss.kettle.controller;

import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

import com.yss.common.core.utils.JdbcUtil;
import com.yss.common.core.utils.easyexecl.EasyExeclUtil;
import com.yss.common.core.utils.uuid.IdUtils;
import com.yss.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yss.common.logger.annotation.Log;
import com.yss.common.logger.enums.BusinessType;
import com.yss.common.security.annotation.RequiresPermissions;
import com.yss.kettle.domain.JobDatasource;
import com.yss.kettle.service.IJobDatasourceService;
import com.yss.common.core.web.controller.BaseController;
import com.yss.common.core.web.domain.AjaxResult;
import com.yss.common.core.web.page.TableDataInfo;

/**
 * ETL数据源信息Controller
 *
 * @author yss
 * @date 2023-04-23
 */
@RestController
@RequestMapping("/datasource")
public class JobDatasourceController extends BaseController {
    @Autowired
    private IJobDatasourceService jobDatasourceService;

    /**
     * 查询ETL数据源信息列表
     */
    @RequiresPermissions("kettle:datasource:list")
    @GetMapping("/list")
    public TableDataInfo list(JobDatasource jobDatasource) {
        startPage();
        List<JobDatasource> list = jobDatasourceService.selectJobDatasourceList(jobDatasource);
        return getDataTable(list);
    }

    /**
     * 导出ETL数据源信息列表
     */
    @RequiresPermissions("kettle:datasource:export")
    @Log(title = "ETL数据源信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JobDatasource jobDatasource) {
        List<JobDatasource> list = jobDatasourceService.selectJobDatasourceList(jobDatasource);
        EasyExeclUtil.export(response, list, JobDatasource.class);
    }

    /**
     * 获取ETL数据源信息详细信息
     */
    @RequiresPermissions("kettle:datasource:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(jobDatasourceService.getById(id));
    }


    /**
     * 新增ETL数据源信息
     */
    @RequiresPermissions("kettle:datasource:add")
    @Log(title = "ETL数据源信息", businessType = BusinessType.OTHER)
    @PostMapping("/testConnection")
    public AjaxResult testConnection(@RequestBody @Validated JobDatasource jobDatasource) {
        Boolean falg = JdbcUtil.testConnection(jobDatasource.getDriverClassName(), jobDatasource.getUrl(),
                jobDatasource.getUsername(), jobDatasource.getPassword());
        if (!falg) {
            return error("测试资源库连接失败！");
        }
        return success("测试资源库连接成功");
    }

    /**
     * 新增ETL数据源信息
     */
    @RequiresPermissions("kettle:datasource:add")
    @Log(title = "ETL数据源信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated JobDatasource jobDatasource) {
        jobDatasource.setId(IdUtils.fastSimpleUUID());
        jobDatasource.setCreateBy(SecurityUtils.getUsername());
        jobDatasource.setCreateTime(new Date());
        if (!"FILE".equals(jobDatasource.getFtype())) {
            Boolean falg = JdbcUtil.testConnection(jobDatasource.getDriverClassName(), jobDatasource.getUrl(),
                    jobDatasource.getUsername(), jobDatasource.getPassword());
            if (!falg) {
                return error("测试资源库连接失败！");
            }
        }
        if (!jobDatasourceService.checkJobDatasourceNameUnique(jobDatasource)) {
            return error("名称【" + jobDatasource.getName() + "】的数据源信息已存在");
        }
        return toAjax(jobDatasourceService.save(jobDatasource));
    }

    /**
     * 修改ETL数据源信息
     */
    @RequiresPermissions("kettle:datasource:edit")
    @Log(title = "ETL数据源信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated JobDatasource jobDatasource) {
        jobDatasource.setUpdateTime(new Date());
        jobDatasource.setUpdateBy(SecurityUtils.getUsername());
        if (!"FILE".equals(jobDatasource.getFtype())) {
            Boolean falg = JdbcUtil.testConnection(jobDatasource.getDriverClassName(), jobDatasource.getUrl(),
                    jobDatasource.getUsername(), jobDatasource.getPassword());
            if (!falg) {
                return error("测试资源库连接失败！");
            }
        }
        if (!jobDatasourceService.checkJobDatasourceNameUnique(jobDatasource)) {
            return error("名称【" + jobDatasource.getName() + "】的数据源信息已存在");
        }
        return toAjax(jobDatasourceService.updateById(jobDatasource));
    }

    /**
     * 删除ETL数据源信息
     */
    @RequiresPermissions("kettle:datasource:remove")
    @Log(title = "ETL数据源信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids) {
        return toAjax(jobDatasourceService.removeByIds(ids));
    }
}
